<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="定时任务管理" @del="del" @newCreate="newCreate"></v-tableTitle>
    <GTable ref="table" api="/v1/job/selectJobList" :columnSource="baseColumns" :cbFun="cbFun" @pause="pause" @resume="resume" @delete="del" @edit="edit" @execute="execute" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'定时任务信息'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'timeTask',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        companyId: null,
        applicationNo: null,
        applicationName: null,
        applicationType: '50201',
        applicationDesc: '',
        applicationStatus: '1'
      },
      statusList: [
        { label: '等待', value: 'WAITING' },
        { label: '暂停', value: 'PAUSED' },
        { label: '阻塞', value: 'BLOCKED' },
        { label: '正在执行', value: 'ACQUIRED' }
      ],
      options:{
        jobs:[],
        apimapping:[]
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate',
          permi: ['timeTask:add']
        }
        // {
        //   label: this.$t('common.delete'),
        //   disabled: true,
        //   invalid: 'del'
        // }
      ]
    },
    baseColumns() {
      return [
        {
          label: '任务名称',
          prop: 'jobName',
          input: 'input'
        },
        {
          label: '任务描述',
          prop: 'description',
          input: 'input'
        },
        {
          label: '任务所在组',
          prop: 'triggerGroup',
          input: 'input'
        },
        {
          label: '任务全路径',
          prop: 'jobClassName',
          input: 'input'
        },
        {
          label: 'cron表达式',
          prop: 'cronExpression',
          input: 'input',
          desc:'不填则为默认触发时间'
        },
        {
          label: '状态',
          prop: 'triggerState',
          input: 'select',
          options: this.statusList
        },      {
          label: "操作",
          prop: "action",
          input: "action",
          width: 360,
          btnConfig: [
            {
              label: "停用",
              invalid: "pause",
              type: "warning",
              disabled: (data) => data.enable == "是",
            },
            {
              label: "启用",
              invalid: "resume",
              type: "info",
              disabled: (data) => data.enable == "否",
            },
            {
              label: "删除",
              invalid: "delete",
              type: "danger"
            },
            {
              label: "修改",
              invalid: "edit",
              type: "success",
              disabled: data => data.enable == '否'
            },
            {
              label: "执行",
              invalid: "execute",
              type: "primary",
              disabled: (data) => data.enable == "是",
            },
          ]
        }
      ]
    },
    formConfig() {
      return [
        {
          prop: 'jobName',
          type: 'input',
          label: '任务名',
          //options: this.options.jobs
        },
        {
          prop: 'jobClassName',
          type: 'input',
          label: '任务类',
          //options: this.options.jobs
        },
        {
          prop: 'jobGroup',
          type: 'input',
          label: '任务分组'
        },
        {
          prop: 'cronExpression',
          type: 'input',
          label: 'cron表达式'
        },
        // {
        //   prop: 'jobClassName',
        //   type: 'input',
        //   label: '任务类'
        // },
        {
          prop: 'description',
          type: 'textarea',
          label: '任务描述'
        },
        {
          prop: 'apiMappingCode',
          type: 'select',
          label: '配置场景编码',
          options: this.options.apimapping
        }
      ]
    },
    formRules() {
      return {
        companyId: [
          {
            required: true,
            message: this.$inputPlease('config.company'),
            trigger: 'change'
          }
        ],
        applicationNo: [
          {
            required: true,
            message: this.$inputPlease('cn.appCode'),
            trigger: 'blur'
          }
        ],
        applicationName: [
          {
            required: true,
            message: this.$inputPlease('cn.appName'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(`/v1/job/editJob`, params)
        } else {
          promise = this.$post(`/v1/job/addJob`, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail?.resetFields()
          this.dialogModule.flag = false
        })
      })
    },
    newCreate() {
      this.dialogModule.title = '新增定时任务'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.form = {
        companyId: null,
        applicationNo: null,
        applicationName: null,
        applicationType: '50201',
        applicationDesc: '',
        applicationStatus: '1'
      }
      this.$refs.detail?.resetFields()
      this.dialogModule.flag = true
    },
    pause(data) {
      this.$post('/v1/job/pauseJob', data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    resume(data) {
      this.$post('/v1/job/resumeJob', data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    del(data) {
      this.$post('/v1/job/deleteJob', data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    edit(data) {
      this.dialogModule.title = '修改定时任务'
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.form = data
      this.$refs.detail?.resetFields()
      this.dialogModule.flag = true
    },
    execute(data) {
      this.$post('/v1/job/executeJob',  data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    closed(){
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    },
    queryjobs(){
      this.$post('/v1/job/selectJobList', { id: '' }).then(res => {
        this.options.jobs = res.data.item;
      })
    },
    queryapimappings(){
      this.$post('/v1/edi/PlApiMapping/selectPlApiMappingSelect', { }).then(res => {
        this.options.apimapping = res.data.item;
      })
    }
  },
  mounted() {
    this.queryjobs();
    this.queryapimappings();
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
